﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PET.Tools;

namespace PET.Controllers
{
    /// <summary>
    /// 宠物护理方式控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CareWayController : ControllerBase
    {
        private readonly PetDbContext db;
        public CareWayController(PetDbContext _context)
        {
            db = _context;

        }

        //查询所有用户护理数据
        [HttpGet]
        public Result Get()
        {
            Result result = new Result();
            result.Data = db.Pet_CareWay.ToList();
            return result;
        }

        //根据id查询信息
        [HttpGet("{id}")]
        public Result GetId(Guid id)
        {
            Result result = new Result();
            result.Data = db.Pet_CareWay.Where(w => w.Id == id).SingleOrDefault();
            return result;
        }

        //用户数据添加
        [HttpPost]
        public Result AddPet([FromBody] Pet_CareWay care)
        {
            Result result = new Result();
            result.Data= db.Pet_CareWay.Add(care);
            result.Data=db.SaveChanges();
            return result;
        }

        //用户数据更改
        [HttpPut]
        public Result Put([FromBody] Pet_CareWay care)
        {
            Result result = new Result();
            result.Data = db.Pet_CareWay.Update(care);
            result.Data = db.SaveChanges();
            return result;
        }

        
        //软删除
        [HttpDelete("{id}")]
        public Result Del(Guid id)
        {
            Result result = new Result();
            Pet_CareWay? careway = new Pet_CareWay();
            careway = db.Pet_CareWay.Where(s => s.Id == id).FirstOrDefault();
            if (careway != null)
            {
                careway.DelTime = DateTime.Now;
                db.Pet_CareWay.Update(careway);
                result.Data = db.SaveChanges();
            }
            return result;
        }
    }
}
